package com.alan.springbootbase.service;


import com.alan.springbootbase.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 *
 */
@Slf4j
@Service
@CacheConfig(cacheNames="user")
public class RedisService {

    @Cacheable(key = "#user.id")
    public User sava(User user) {

        log.debug("------------- Redis sava----------------");
        user.setCode("001");
        user.setName("sava");
        user.setAge(1);
        return user;
    }

    @CachePut(key = "#user.id")
    public User update(User user) {
        log.debug("------------- Redis update----------------");
        user.setCode("001");
        user.setName("update");
        user.setAge(1);
        return user;
    }

    @Cacheable(key = "#id")
    public User findById(String id) {
        log.debug("------------- Redis findById----------------");
        User user =new User();
        user.setId(id);
        user.setCode("001");
        user.setName("findById");
        user.setAge(1);
        return user;
    }

    @CacheEvict(key = "#id")
    public boolean del(String id) {

        log.debug("------------- del Redis -----------------id:"+id);
        return true;
    }



}
